home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-12-17 | 12.2 KB | 354 lines | [TEXT/MPS ] |
- #
- # ****************************************************************************
- #
- # File Name: String.Lib
- #
- # Contains: xxx put contents here xxx
- #
- # Written by: Kevin Avoy, Ken Landreth, Michael Leong, Gil Spencer et al
- #
- # Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
- #
- # ****************************************************************************
- # C h a n g e H i s t o r y (most recent first):
- # ****************************************************************************
- #
- # Vers Date Author Description
- # ---- -------- ------ ---------------------------------------------
- # <1.0.2> 8/20/93 KTA Added StringUntilChar(), StripCarriageReturn(),
- # NumTimesCharInString(), StringAfterChar() - for better filepath
- # parsing.
- # <1+> 5/21/93 NAGA Adding header and porting old files to follow new standards
- #
- # ****************************************************************************
- #
-
- #########################################################################
- # IsSubString(str1, str2)
- #=======================================================================
- # Author: SL
- # Description: Checks to see if str1 is in str2.
- # Parameters: str1 := Substring to be searched for.
- # str2 := String.
- # Returns: 0 := str1 is not in str2.
- # 1 := str1 is in str2.
- #=======================================================================
- # History:
- #
- #########################################################################
- TASK IsSubString(str1, str2) begin
- if (str2 = str1) or ( str2 ~= /≈"{str1}"≈/) return(1);
- else return(0);
- end; # isSubString()
-
- #########################################################################
- # PointListToStr(pts)
- #=======================================================================
- # Author: DM
- # Description: Converts a list of points to a string.
- # Parameters: pt := List of coordinate pairs.
- # Returns: 0 := if the conversion fails.
- # 1 := The pt as a string.
- #=======================================================================
- # History:
- #
- #########################################################################
- TASK PointListToStr(pointList := {}) begin
- returnVal := O;
- if(pointList) begin
- pointStr := "";
- pointCount := card(pointList);
- for index := 1 to (pointCount-1) begin
- point := pointList[index];
- x := point[1];
- y := point[2];
- pointStr := "{pointStr}∂{{x},{y}∂},";
- end;
- point := pointList[index];
- x := point[1];
- y := point[2];
- pointStr := "∂{{pointStr}∂{{x},{y}∂}∂}";
- returnVal := pointStr;
- end;
- else
- Println "The list passed to PointListToStr is empty";
- return(returnVal);
- end; # PtToStr()
-
- #########################################################################
- # FindPos(char, str, startPos)
- #=======================================================================
- # Author: SL
- # Description: Finds the first occurence of char in str starting
- # from startPos.
- # Parameters: char := A character to be searched for.
- # str := String.
- # startPos:= Starting position to be searched.
- # Returns: 0 := Char is not in str starting from
- # startPos.
- # pos := Char position of its first occurence
- # starting from startPos.
- #=======================================================================
- # History:
- #
- #########################################################################
- TASK FindPos(char, str, startPos:= 1)
- begin
- numStr := card str;
-
- # If start position is greater than the string length, return 0 #
- if (startPos > numStr)
- return(0);
-
- for i:= startPos to numStr do
- begin
- if (char = str[i])
- return(i);
- end; # for
- return(0);
- end; # findPos()
-
- #########################################################################
- # Substring(String,StartChar,NumChar)
- #=======================================================================
- # Author: PF
- # Description: returns the first NumChar characters of the passed String
- # starting at StartChar.
- # Parameters: String - String to use
- # StartChar - Character to start making substring from
- # NumChar - Integer number of characters to return
- # Returns: string
- # Assumptions: none
- #=======================================================================
- # History:
- #
- ##########################################################################
- TASK Substring(String,StartChar,NumChar) begin
- # first see if number of characters is greater then length of string
- # and return the whole end of the string if it is.
- if (NumChar > ((cardString ) - StartChar + 1)) begin
- return (Substring (String,StartChar,((cardString) - StartChar) +1));
-
- end;
- else begin
- # create a string of first NumChar characters and return it.
- NewString:=""; #start with an empty string
- for CharCount :=StartChar to ((StartChar + NumChar) - 1)
- NewString := NewString + String[CharCount];
- return (NewString);
- end;
- end; #Substring Task
-
- #########################################################################
- # RandomString( NumChar )
- #========================================================================
- # Author: Kevin Avoy (ext. 45604)
- # Description: Generate Random strings <NumChar> long.
- # Parameters: NumChar := Length of random string to generate
- # Returns: The random string.
- # Examples: RandomString( 5 ); - Random String 5 characters in length
- # Assumptions: That you want Caps, lower case, numbers and punctuation
- #========================================================================
- # History:
- #
- #########################################################################
- TASK RandomString(NumChar := 3)
- begin
- RandStr := '';
- #FullcharSTRING := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&()_-=+{[}]"';></?`\'~; # All Caps
- ALLCAPSString := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; # ALL CAPS
- lowercaseString := 'abcdefghijklmnopqrstuvwxyz'; # lower case
- NumberString := '0123456789'; # Numbers
- PunctuationString := " !@#$%^&()_-∂{∂}'~`"; # Punctuaton
-
- charSTRING := ALLCAPSString + lowercaseString + NumberString + PunctuationString;
- for NumTimes := 1 to NumChar begin # How many characters
- WhichItem := Random(1,Card charSTRING); # Get a random index into the charString
- NewChar := charSTRING[WhichItem]; # Get a character
- RandStr := RandStr + NewChar; # Concat to the String
- end;
- Return(RandStr);
- end; # RandomString()
-
- #############################################################################
- TASK FormPositiveInteger(str, start_index := 1, end_index := -1)
- begin
- num := 0;
- if (end_index = -1) end_index := card(str);
- for i:= start_index to end_index
- begin
- digit := CharToDigit(str[i]);
- if (digit = -1) return -1; #error condition
- else if (num > 3276) return -1; #overflow condition
- else if (num = 3276) and (digit > 7) return -1; #overflow condition
- num := num*10 + digit;
- end;#for each digit
- return num;
- end;
- #############################################################################
- TASK CharToDigit(character)
- begin
- if (character = '0')
- return (0);
- else if (character = '1')
- return(1);
- else if (character = '2')
- return(2);
- else if (character = '3')
- return(3);
- else if (character = '4')
- return(4);
- else if (character = '5')
- return(5);
- else if (character = '6')
- return(6);
- else if (character = '7')
- return(7);
- else if (character = '8')
- return(8);
- else if (character = '9')
- return(9);
- else return (-1); #error condition
- end;
-
- #########################################################################
- # StripCarriageReturn(pLine)
- #========================================================================
- # Author: Kevin Avoy (x4-5604)
- # Description: returns a string that contains all of the characters in
- # <pLine> up to but not including the carriage return.
- # Parameters: pLine - The line of text
- # Returns: string without the carriage return
- # Examples: StripCarriageReturn();
- # Assumptions: None
- #========================================================================
- # History:
- #
- #########################################################################
- TASK StripCarriageReturn(pLine)
- begin
- return(StringUntilChar(pLine, "∂n",0));
- end;
-
- #########################################################################
- # StringUntilChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
- #========================================================================
- # Author: Kevin Avoy (x4-5604)
- # Description: returns a string that contains all of the characters in
- # <pTheString> up to <pTheChar>. If <pIncludeTheChar> evaluates to
- # true <pTheChar> will be included in the returned string.
- # Parameters: pTheString - The string
- # pTheChar - the character to search for
- # pIncludeTheChar - Boolean indicates whether or not to include
- # <pTheChar>.
- # pNumOccurences - indicates how many times <pChar> should occur
- # prior to returning the string.
- # Returns: returns a string that contains all of the characters in
- # <pTheString> up to (but not necessarily including) <pTheChar>.
- # Examples: StringUntilChar();
- # Assumptions: None
- #========================================================================
- # History:
- #
- #########################################################################
- TASK StringUntilChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
- begin
- newString := '';
- actualOccurrence := 0;
- if(pTheString)
- begin
- for i := 1 to Card(pTheString)
- begin
- if not (pTheString[i] = pTheChar)
- newString := newString + pTheString[i];
- else
- begin
- actualOccurrence := actualOccurrence +1;
- if (actualOccurrence = pNumOccurences)
- begin
- if(pIncludeTheChar)
- newString := newString + pTheString[i];
-
- return (newString);
- end;
- else
- newString := newString + pTheString[i];
- end;
- end;
- end;
- return (newString);
- end;
-
- #########################################################################
- # NumTimesCharInString(pTheString, pTheChar)
- #========================================================================
- # Author: Kevin Avoy (x4-5604)
- # Description: returns an integer that indicates the number of times <pChar>
- # occurs in <pTheString>.
- # Parameters: pTheString - The string
- # pTheChar - the character to search for
- # Returns: returns an integer that indicates the number of times <pChar>
- # occurs in <pTheString>.
- # Examples: NumTimesCharInString("hd:thisFolder:ThatFolder:FileName", ":");
- # Assumptions: None
- #========================================================================
- # History:
- #
- #########################################################################
- TASK NumTimesCharInString(pTheString, pTheChar)
- begin
- actualOccurrence := 0;
- for i := 1 to Card(pTheString)
- begin
- if (pTheString[i] = pTheChar)
- actualOccurrence := actualOccurrence +1;
- end;
- return (actualOccurrence);
- end;
-
- #########################################################################
- # StringAfterChar(pTheString, pTheChar, pIncludeTheChar, pNumOccurences)
- #========================================================================
- # Author: Kevin Avoy (x4-5604)
- # Description: returns a string that contains all of the characters in
- # <pTheString> after <pTheChar>. If <pIncludeTheChar> evaluates to
- # true <pTheChar> will be included in the returned string.
- # Parameters: pTheString - The string
- # pTheChar - the character to search for
- # pIncludeTheChar - Boolean indicates whether or not to include
- # <pTheChar>.
- # pNumOccurences - indicates how many times <pChar> should occur
- # prior to creating the returned string.
- # Returns: returns a string that contains all of the characters in
- # <pTheString> after <pTheChar>.
- # Examples: StringAfterChar();
- # Assumptions: None
- #========================================================================
- # History:
- #
- #########################################################################
- TASK StringAfterChar(pTheString, pTheChar, pIncludeTheChar := 0, pNumOccurences := 1)
- begin
- newString := '';
- actualOccurrence := 0;
- for i := 1 to Card(pTheString)
- begin
- if not(CreateStringFlag)
- begin
- if (pTheString[i] = pTheChar)
- begin
- actualOccurrence := actualOccurrence +1;
- if(pNumOccurences = actualOccurrence)
- begin
- if(pIncludeTheChar)
- newString := newString + pTheString[i];
- CreateStringFlag := 1;
- end;
- end;
- end;
- else
- newString := newString + pTheString[i];
- end;
- return (newString);
- end;
-